class Solution {
public:
    int largestSumAfterKNegations(vector<int>& nums, int k) {
        sort(nums.begin(),nums.end());
        int sum=0;
        for(int i=0;i<nums.size();i++)
        {
            if(k==0)
                break;
            if(nums[i]<0)
            {
                nums[i]=~nums[i]+1;
                k--;
            }
            else
            {
                sort(nums.begin(),nums.end());
                while(k)
                {
                    nums[0]=~nums[0]+1;
                    k--;
                }
                break;
            }
        }
        sort(nums.begin(),nums.end());//处理数组全部位负数，将负数全部转为正数后，跳出循环，k依旧大于0
        while(k>0)
        {
            nums[0]=~nums[0]+1;
            k--;
        }
        for(auto num:nums)
        {
            sum+=num;
        }
        return sum;
    }
};